package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.entity.DecorationTag;
import com.ruoyi.mapper.DecorationTagMapper;
import com.ruoyi.service.DecorationTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 装饰画标签关联服务实现类
 *
 * @author ruoyi
 */
@Service
@Slf4j
public class DecorationTagServiceImpl extends ServiceImpl<DecorationTagMapper, DecorationTag> implements DecorationTagService {

    /**
     * 根据装饰画ID获取关联的标签列表
     *
     * @param decorationId 装饰画ID
     * @return 关联的标签列表
     */
    @Override
    public List<DecorationTag> listTagsByDecorationId(Integer decorationId) {
        LambdaQueryWrapper<DecorationTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationTag::getDecorationId, decorationId);
        return list(wrapper);
    }

    /**
     * 添加装饰画与标签的关联
     *
     * @param decorationId 装饰画ID
     * @param tagId        标签ID
     * @return 是否成功添加关联
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addTag(Integer decorationId, Integer tagId) {
        // 先查询是否已存在关联
        LambdaQueryWrapper<DecorationTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationTag::getDecorationId, decorationId)
                .eq(DecorationTag::getTagId, tagId);
        DecorationTag existing = getOne(wrapper);
        if (existing != null) {
            log.info("装饰画与标签关联已存在，decorationId={}，tagId={}", decorationId, tagId);
            return true;
        }

        // 创建新关联
        DecorationTag decorationTag = new DecorationTag();
        decorationTag.setDecorationId(decorationId);
        decorationTag.setTagId(tagId);
        return save(decorationTag);
    }

    /**
     * 移除装饰画与标签的关联
     *
     * @param decorationId 装饰画ID
     * @param tagId        标签ID
     * @return 是否成功移除关联
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeTag(Integer decorationId, Integer tagId) {
        LambdaQueryWrapper<DecorationTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationTag::getDecorationId, decorationId)
                .eq(DecorationTag::getTagId, tagId);
        return remove(wrapper);
    }

    /**
     * 批量添加装饰画与标签的关联
     *
     * @param decorationId 装饰画ID
     * @param tagIds       标签ID列表
     * @return 是否成功批量添加关联
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddTags(Integer decorationId, List<Integer> tagIds) {
        if (tagIds == null || tagIds.isEmpty()) {
            return true;
        }

        // 查询现有关联
        LambdaQueryWrapper<DecorationTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationTag::getDecorationId, decorationId)
                .in(DecorationTag::getTagId, tagIds);
        List<DecorationTag> existingTags = list(wrapper);

        // 过滤出需要新增的标签ID
        List<Integer> existingTagIds = existingTags.stream()
                .map(DecorationTag::getTagId)
                .collect(java.util.stream.Collectors.toList());

        List<DecorationTag> newTags = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();

        for (Integer tagId : tagIds) {
            if (!existingTagIds.contains(tagId)) {
                DecorationTag decorationTag = new DecorationTag();
                decorationTag.setDecorationId(decorationId);
                decorationTag.setTagId(tagId);
                newTags.add(decorationTag);
            }
        }

        return newTags.isEmpty() || saveBatch(newTags);
    }

    /**
     * 批量移除装饰画与标签的关联
     *
     * @param decorationId 装饰画ID
     * @param tagIds       标签ID列表
     * @return 是否成功批量移除关联
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchRemoveTags(Integer decorationId, List<Integer> tagIds) {
        if (tagIds == null || tagIds.isEmpty()) {
            return true;
        }

        LambdaQueryWrapper<DecorationTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationTag::getDecorationId, decorationId)
                .in(DecorationTag::getTagId, tagIds);
        return remove(wrapper);
    }
}
